/* Filename: adc.c
   Author: Chris Chester
   Date: 20/09/2012
   Notes: This code was adapted from code created by Andrew Nicholas
   
   Discription: The main code designed to be operated with a PIC micro
   to work with the bioimpedance circuit.
*/
#define FCY 500000UL
#include <libpic30.h> 
#include <p30f3013.h>
#include <stdlib.h>
#include <stdio.h>

#include "debug_led.h"
#include "uart_bisskit.h"
#include "adc.h"

#define SYNC_BYTE 0xFF

#define BORV_SET 0x30 // Brown out voltage set to not in operating range

#define ADC_MAX 0xFD

#define DOWNSAMPLE_TIME 100	// Convert from 9.4kHz to 50Hz
#define HIGH_BIT_SHIFT 8 		// Shift needed to convert highest 8 bits of a conversion down 

#define TRANSFER_DELAY 200

//Channel 1 data
uint16_t adc_data_ch1 = 0;
uint8_t high_byte_ch1 = 0;
uint8_t low_byte_ch1 = 0;

// Channel 2 data
uint16_t adc_data_ch2 = 0;
uint8_t high_byte_ch2 = 0;
uint8_t low_byte_ch2 = 0;

uint16_t timer_count_1 = 0;

// Inital Microcontroller Configuration
_FOSC(CSW_FSCM_OFF & HS3_PLL16);
_FWDT(WDT_ON & WDTPSA_512 & WDTPSB_16);
_FBORPOR(PBOR_ON & BORV_SET & PWRT_64 & MCLR_EN);
_FGS(CODE_PROT_OFF);
_ICD(ICS_PGD1);

/* Function prototypes */
void UART_test();
void ADC_test();

int main (void){
	__delay_ms(10000); //To allow for micro to be set-up before UART connection is attempted
// Initialise LED's for debugging 
	led_init();

// Initialise UART and ADC
    uart_init(); 
    adc_init();
	     
	     
	while(1){

       // timer_counts are used to downsample the data. This sends data at ~10 Hz
        if(timer_count_1 >= DOWNSAMPLE_TIME){
                // ADC data retrieval, and UART transmit with one channel takes 184us to complete and
                // with two channels 458us to complete.  
                if (adc_is_new_data())
                {
                    adc_data_ch1 = adc_read_ch1();
                    
                    // Check to make sure the data value isn't the same as a sync bit
                    if(adc_data_ch1 == SYNC_BYTE)
                    {
	                	adc_data_ch1 = ADC_MAX;	    
	                } 
                    
                    low_byte_ch1 = adc_data_ch1;
                    high_byte_ch1 = adc_data_ch1 >> HIGH_BIT_SHIFT;
                    
                    adc_data_ch2 = adc_read_ch2();
                    
                    // Check to make sure the data value isn't the same as a sync bit
                    if(adc_data_ch2 == SYNC_BYTE)
                    {
	                	adc_data_ch2 = ADC_MAX;	    
	                } 
	                
                    low_byte_ch2 = adc_data_ch2;
                    high_byte_ch2 = adc_data_ch2 >> HIGH_BIT_SHIFT; 
                    
                    //---- Send data
                    
//                    UART_test();        
                	/*low_byte_ch1 = 0x01;
                	high_byte_ch1 = 0x00;
                	low_byte_ch2 = 0x03;
                	high_byte_ch2 = 0x00;*/
                    
                    uart_send(SYNC_BYTE);
                    // Channel 1 data
                    __delay_ms(TRANSFER_DELAY);
                    uart_send(low_byte_ch1);
                    __delay_ms(TRANSFER_DELAY);
                    uart_send(high_byte_ch1);
                    
                    // Channel 2 data
                   	__delay_ms(TRANSFER_DELAY);
                   	uart_send(low_byte_ch2);
                   	__delay_ms(TRANSFER_DELAY);
                   	uart_send(high_byte_ch2);
                    
                    adc_set_data_received(0); // turn flag off to indicate data recieved.                    
                }
                
                // Reset timers
                timer_count_1 = 0;
        }
      timer_count_1++;   
	}	
}
/* UART_test
Test to determine if the UART is recieving certain info
===========================TO CHECK===================
*/
void UART_test(){
       led_yellow_on();
	   uart_send(SYNC_BYTE);

      // uart_send(0x20); // ASCII 0x41 = A
	   uart_send(0x45); // ASCII 0x45 = E
       uart_send(0x48); // ASCII 0x48 = H   Final Value = 0x4845 => 18501

//	   uart_send(SYNC_BYTE); 
//      // uart_send(0x20); // ASCII 0x41 = A
//	   uart_send(0x47);  // ASCII 0x47 = G 
//       uart_send(0x46);  // ASCII 0x46 = F  Final Value = 0x4647 => 17991
       led_yellow_off();
}

/* ADC_test
Test to determine if the ADC is recieving certain info
===========================TO CHECK===================
*/
void ADC_test(){

		if (low_byte_ch1 > 0x07) {
			led_yellow_on();
		}
		else{
			led_yellow_off();
		}
		if (high_byte_ch1 > 0x04){
			led_green_on();
		}
		else{
			led_green_off();
		}
}
